package com.angel.util;

import java.util.HashMap;
import java.util.List;

import com.weibo.net.Weibo;
import com.weibo.net.WeiboDialogListener;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class UserInfoUtil {
	public static final String USER_TABLE="user_table";
	public static SharedPreferences pref;
	/*判断PS中是否存储了该字段*/
	public static Boolean isExit(Context context,String key){
		pref=context.getSharedPreferences(USER_TABLE,Context.MODE_WORLD_READABLE);
		if(pref.contains(key)){
			return true;
		}
		else
		return false;
	}
	/*判断PS中是否存储了该List集合*/
	public static Boolean isExit(Context context,List<String> key){
		return false;
	}
	/*保存要存储的值HashMap到Preference*/
	public static void setValue(Context context,HashMap<String,String> map){
		 SharedPreferences.Editor editor=context.getSharedPreferences(UserInfoUtil.USER_TABLE, Context.MODE_WORLD_WRITEABLE)
					.edit();
		 for(String key:map.keySet()){
			 editor.putString(key,map.get(key));
		 }
		 editor.commit();
	}
	/*保存要存储的值Integer到Preference*/
	public static void setValue(Context context,String key,Integer value){
		 SharedPreferences.Editor editor=context.getSharedPreferences(UserInfoUtil.USER_TABLE, Context.MODE_WORLD_WRITEABLE)
					.edit();
		 editor.putInt(key, value);
		 editor.commit();
	}
	/**获取存储在Preference的值，不存在则设置默认值
	 * @param context 
	 * @param key  关键字
	 * @param defaultValue 默认值
	 * @return 返回存储在USER_TABLE表key对应的String类型值;不存在，则返回默认值
	 */
	public static String getValue(Context context,String key,String defaultValue){
		pref=context.getSharedPreferences(USER_TABLE,Context.MODE_WORLD_READABLE);
	    return pref.getString(key, defaultValue);
	}
	/**获取存储在Preference的值，不存在则设置默认值
	 * @param context 
	 * @param key  关键字
	 * @param defaultValue 默认值
	 * @return 返回存储在USER_TABLE表key对应的Integer类型值;不存在，则返回默认值
	 */
	public static Integer getValue(Context context,String key,Integer defaultValue){
		pref=context.getSharedPreferences(USER_TABLE,Context.MODE_WORLD_READABLE);
	    return pref.getInt(key, defaultValue);
	}
	
	  /*用于判断是否授权或是否过期*/
    public static Boolean judgeOauth(Context context,String key){
  	   SharedPreferences pre=context.getSharedPreferences(USER_TABLE,Context. MODE_WORLD_WRITEABLE);
 	       String expire_in=pre.getString("weibo_expire_in", "0");
 	       long expire_time=Integer.parseInt(expire_in);
 	       long oauthTime=pre.getLong("oauthTime", 0);
 	       long currentTime=BaseUtil.getTime();
 	       
         float outDate=(currentTime-oauthTime)/1000;
 	       Log.i("TAG", "outDate:"+String.valueOf(outDate));
 	       Log.i("TAG", "expire_time:"+String.valueOf(expire_time));
         //判断该键值存在否
 	       if(pre.contains(key)){
 	    	   //如果超过access_token过期时间,这里是判断过期5秒后才能提醒
 	    	   if((outDate+5>=expire_time)){
 	    		   return false;
 	    	   }
 	    	   return true;
 	       }
 	       else{
 	    	   return false;
 	       }
    }
    /*认证请求*/
    public static void oauthRequest(Context context,WeiboDialogListener listener){
    	   Weibo weibo = Weibo.getInstance();
				weibo.setRedirectUrl("http://www.baidu.com");
				weibo.authorize((Activity) context,listener);
      }
    /*清空微博登录记录*/
    public static void clearWeiboInfo(Context context){
    	  SharedPreferences.Editor editor=context.getSharedPreferences(UserInfoUtil.USER_TABLE, Context.MODE_WORLD_WRITEABLE)
					.edit();
    	  editor.putString("uid", "");
    	  editor.putString("weibo_access_token", "");
		  editor.putString("access_token_secret", "");
		  editor.putString("weibo_expire_in", "0");//存储离过期的时间
		  editor.putLong("oauthTime", 0);//保存当前时间
		  editor.commit();
    }
    /*清空qq登录记录*/
    public static void clearQQInfo(Context context){
  	  SharedPreferences.Editor editor=context.getSharedPreferences(UserInfoUtil.USER_TABLE, Context.MODE_WORLD_WRITEABLE)
					.edit();
	  	editor.putString("openid", "");
		editor.putString("qq_access_token","");
		editor.putLong("qq_expires_in",0);
	    editor.commit();
    }
    
    /**清空用户信息，提供用户切换账号
     * @param context 上下文
     */
    public static void clearUserMsg(Context context){
        SharedPreferences.Editor editor=context.getSharedPreferences(UserInfoUtil.USER_TABLE, Context.MODE_WORLD_WRITEABLE)
					.edit();
        editor.clear();
        editor.commit();
    }
}
